﻿namespace FietsenManagementJB
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Script.Serialization;

    static class IOModule
    {
        public static SortableBindingList<Bike> readDBFile(SortableBindingList<Bike> bikes)
        {
            try
            {
                var sr = new StreamReader("BikeDB.txt");
                var oDeSerializer = new JavaScriptSerializer();
                bikes = oDeSerializer.Deserialize<SortableBindingList<Bike>>(sr.ReadToEnd());
                sr.Close();
                return bikes;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public static bool writeDBFile(SortableBindingList<Bike> bikes)
        {
            var oSerializer = new JavaScriptSerializer();
            try
            {
                var sJSON = oSerializer.Serialize(bikes);
                var tw = new StreamWriter("BikeDB.txt");
                tw.Write(sJSON);
                tw.Close();
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public static bool fileExists(string filePath)
        {
            return File.Exists(filePath);
        }

        public static bool copyImageFile(string newFilePath, string oldFilePath)
        {
            try
            {
                File.Copy(oldFilePath, newFilePath);
            }
            catch(Exception e)
            {
                return false;
            }
            return true;
        }
    }
}
